package Framework;
// Keyboard object, it's a keyboard. Go figure. 

import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collections;
import java.util.Iterator;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class dlg_Keyboard extends dlg_Ancestor implements ActionListener {
	
	btn_Ancestor	btn_iteratedButton;
	JTextField jtf_input = new JTextField("");
	
	static String is_QuertyLayout = "1234567890QWERTYUIOPASDFGHJKLZXCVBNM ";
	Vector <btn_Ancestor> btn_Suggestions = new Vector<btn_Ancestor>();
	pnl_Scrollable pnl_Suggestions = new pnl_Scrollable(new Rectangle(200,0,400, 300), new Rectangle(0,0,400,300), false);
	
	boolean ib_SuggestionsOn = false;
	 
	btn_Ancestor btn_Ok 			= new btn_Ancestor("Ok"); 			// Used for Ok / Yes / 'Do it' / emotive 
	btn_Ancestor btn_Cancel 		= new btn_Ancestor("Cancel"); 	// Oppoisite
	
	public void addSuggestions(Vector<String> as_Vector){
		btn_Ancestor lbtn_IteratorButton;
		for (int i = 0; i < as_Vector.size(); i++){
			
			lbtn_IteratorButton = new btn_Ancestor(as_Vector.elementAt(i));
			lbtn_IteratorButton.addActionListener(this);
			btn_Suggestions.add(lbtn_IteratorButton);
			
		}
		
		ib_SuggestionsOn = true;
		loadSuggestions();
		this.add(pnl_Suggestions);
	}
	public void addSuggestions(){
		String ls_Letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		btn_Ancestor lbtn_IteratorButton;
		
		for (int i = 0; i < ls_Letters.length(); i++){
			
			lbtn_IteratorButton = new btn_Ancestor(ls_Letters.charAt(i) + "Suggestion1abc");
			lbtn_IteratorButton.addActionListener(this);
			btn_Suggestions.add(lbtn_IteratorButton);
			
			lbtn_IteratorButton = new btn_Ancestor(ls_Letters.charAt(i) + "Suggestion2abc");
			lbtn_IteratorButton.addActionListener(this);
			
			btn_Suggestions.add(lbtn_IteratorButton);
		}
		
		ib_SuggestionsOn = true;
		loadSuggestions();
		this.add(pnl_Suggestions);
	}

	public dlg_Keyboard(String as_Question) {
		// Calls overloaded, basically allows querty keyboard without boolean flag.
		this(as_Question, false);
	}
	public void SetOkText(String as_Text){
		// Used to set the button to somethign else eg 'Save'
		
	}
	public dlg_Keyboard(String as_Question, boolean numericOnly) {
		super();	
		
		// Creating single line edit
		jtf_input.setBounds(200,0,400, 50);
		
		btn_Ok.addActionListener(this);
		btn_Cancel.addActionListener(this);
		btn_Ok.setBounds(new Rectangle(600,0,150,50));
		btn_Cancel.setBounds(new Rectangle(50,0,150,50));
		this.add(btn_Ok);
		this.add(btn_Cancel);
		this.add(jtf_input);
		
		int	li_ButtonStartX = 0;
		int li_ButtonStartY = 300;
		int li_Line = 0;
		int li_Key 	= 0;
		
		Rectangle lrect_ButtonBounds = new Rectangle(li_ButtonStartX,li_ButtonStartY,80,80);
		
		// Adding buttons
		if (numericOnly){
			// Adding 0 at the end ;)
			for (int i = 8; i > 0; i--){
				if (li_Key == 3){
					li_Line+= 1;
					lrect_ButtonBounds.x = li_ButtonStartX;  
					lrect_ButtonBounds.y = li_ButtonStartY + li_Line * lrect_ButtonBounds.height;
				}
				btn_iteratedButton = new btn_Ancestor(is_QuertyLayout.substring(i, i+1));
				btn_iteratedButton.addActionListener(this);
				btn_iteratedButton.setBounds(lrect_ButtonBounds);
				
				li_Key += 1;
				lrect_ButtonBounds.x = li_Key * lrect_ButtonBounds.width;
				
				this.add(btn_iteratedButton);
				
			}
		} else {
			for (int i = 0; i < is_QuertyLayout.length() - 1; i++){
				btn_iteratedButton = new btn_Ancestor( is_QuertyLayout.substring(i, i+1) );
				btn_iteratedButton.setBounds(lrect_ButtonBounds);
				btn_iteratedButton.addActionListener(this);
	
				li_Key += 1;
				if (li_Key == 10 && li_Line <= 1){
						li_Key = 0;
						li_Line += 1;
				} else if (li_Key == 9 && li_Line == 2){
						li_Line += 1;
						li_Key = 0;
				} 
				if (li_Line > 1 && li_Key == 0){
					li_ButtonStartX += (lrect_ButtonBounds.width / 2);
				}
				
				lrect_ButtonBounds.x = li_ButtonStartX + li_Key * lrect_ButtonBounds.width;
				lrect_ButtonBounds.y = li_ButtonStartY + li_Line * lrect_ButtonBounds.height;
				
				this.add(btn_iteratedButton);
		
			}
		}
		
	}
	
	public void loadSuggestions(){
		if (ib_SuggestionsOn){
			pnl_Suggestions.clear();
			int	li_boundsCount= 0;
			
			for (int i = 0; i < btn_Suggestions.size(); i++){
				btn_Ancestor lbtn_next = btn_Suggestions.elementAt(i);
				
				if (lbtn_next.getText().toUpperCase().contains(jtf_input.getText())){
					
					lbtn_next.setBounds(0,(50*li_boundsCount),400,50);
					li_boundsCount ++;
					System.out.println("True");
					pnl_Suggestions.add(lbtn_next);
				}
				pnl_Suggestions.validate();
				pnl_Suggestions.i_InternalSize = new Rectangle(0,0,400,(50*li_boundsCount));
			}		
		}
	}
	public void actionPerformed(ActionEvent e) {
		super.actionPerformed(e);
		btn_Ancestor ActionSource = (btn_Ancestor) e.getSource();
		if ( ActionSource.equals(btn_Ok) ){
			deactivate();
		} else if ( ActionSource.equals(btn_Cancel) ){
			deactivate();
		}
		
		if ( is_QuertyLayout.contains(ActionSource.getText() ) ){
			jtf_input.setText(jtf_input.getText() + ActionSource.getText());
		
			if (ib_SuggestionsOn){
				loadSuggestions();
			}
		} 
		
		
		
		
	}
	
}